package com.xuecheng.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.exception.XuechengOnlineException;
import com.xuecheng.mapper.CourseBaseMapper;
import com.xuecheng.mapper.CourseTeacherMapper;
import com.xuecheng.po.CourseBase;
import com.xuecheng.po.CourseTeacher;
import com.xuecheng.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Autowired
    CourseBaseMapper courseBaseMapper;
    /**
     * 查询教师
     *
     * @param id
     * @return
     */
    @Override
    public List<CourseTeacher> selectCourseTeacherById(Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,id);
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(queryWrapper);
        return courseTeacherList;
    }

    /**
     * 添加教师和修改教师
     *  通过id，有id--修改教师，  没有id--添加教师
     * @param company       机构id
     * @param courseTeacher
     */
    @Transactional
    @Override
    public CourseTeacher saveCourseTeacher(Long company, CourseTeacher courseTeacher) {
        Long courseId = courseTeacher.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        //只允许向机构自己的课程中添加教师
        if(!company.equals(courseBase.getCompanyId())){
            throw new XuechengOnlineException("只允许向机构自己的课程添加教师");
        }
        Long id = courseTeacher.getId();
        if(id == null){
            //添加教师
            CourseTeacher teacher = new CourseTeacher();
            teacher.setCreateDate(LocalDateTime.now());
            BeanUtils.copyProperties(courseTeacher,teacher);
            int insert = courseTeacherMapper.insert(teacher);
            if(insert <= 0){
                throw new XuechengOnlineException("新增教师失败");
            }
            return teacher;
        }else{
            //修改教师
            CourseTeacher teacher = courseTeacherMapper.selectById(id);
            BeanUtils.copyProperties(courseTeacher,teacher);
            int i = courseTeacherMapper.updateById(teacher);
            if(i <= 0){
                throw new XuechengOnlineException("修改教师失败");
            }
            return teacher;
        }
    }

    /**
     * 删除教师
     * @Param company 机构id
     * @param courseId  课程id
     * @param teacherId  教师id
     */
    @Override
    public void deleteCourseTeacher(Long company,Long courseId, Long teacherId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        //只允许向机构自己的课程中添加教师
        if(!company.equals(courseBase.getCompanyId())){
            throw new XuechengOnlineException("只允许向机构自己的课程删除教师");
        }
        int i = courseTeacherMapper.deleteById(teacherId);
        if(i < 0){
            throw new XuechengOnlineException("删除教师失败");
        }
    }
}
